package com.wuseguang.springcloud.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wuseguang.springcloud.dao.jdbc.QuestionnaireJdbc;
import com.wuseguang.springcloud.dao.mapper.QuestionnaireMapper;
import com.wuseguang.springcloud.dao.repository.QuestionnaireRepository;
import com.wuseguang.springcloud.dao.repository.QuestionnaireTitleRepository;
import com.wuseguang.springcloud.dao.repository.QuestionnaireUsersRepository;
import com.wuseguang.springcloud.entity.Questionnaire;
import com.wuseguang.springcloud.entity.QuestionnaireTitle;
import com.wuseguang.springcloud.service.QuestionnaireService;
import com.wuseguang.springcloud.util.MapUtil;
import com.wuseguang.springcloud.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 问卷调查表 服务实现类
 * </p>
 *
 * @author 
 * @since 2020-06-02
 */
@Service
public class QuestionnaireServiceImpl  implements QuestionnaireService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private QuestionnaireJdbc questionnaireJdbc;
    @Autowired
    private QuestionnaireRepository questionnaireRepository;
    @Autowired
    private QuestionnaireTitleRepository questionnaireTitleRepository;
    @Autowired
    private QuestionnaireUsersRepository questionnaireUsersRepository;

    @Override
    public Result addQuestionnaire(Questionnaire questionnaire) {
        //时间
        questionnaire.setAddTime(new Date());
        //问卷id
        Integer id = questionnaireRepository.save(questionnaire).getId();

        //解析题目信息
        JSONArray list = JSONObject.parseArray(questionnaire.getSubject());
        //选好题目信息
        for (Object string : list) {
            Map<String, Object> map = JSON.parseObject(string.toString());

            //创建题目信息
            QuestionnaireTitle  qt = new QuestionnaireTitle();
            qt.setTitle(map.get("title").toString());
            qt.setQuestionnaireOption(map.get("option").toString());
            qt.setQuestionnaireId(id);
            questionnaireTitleRepository.save(qt);
        }

        //添加问卷调查学生信息
        String sql = "INSERT INTO `questionnaire_users`(`student_id`, `questionnaire_type`, `questionnaire_id`) VALUES (?, ?, ?)";
        questionnaireJdbc.insetAndUpdateQuestionnaireUsers(sql,questionnaire.getStudentIds(),questionnaire.getQuestionnaireType(),id);

        return new Result();
    }

    @Override
    public Result findQuestionnaire(String teacherId, String schoolId) {

        //查询教师发布的问卷调查
        String sql = "select q.id,q.survey_name ,q.survey_description,qt.questionnaire_option," +
                " q.question_type,DATE_FORMAT(q.add_time,'%Y-%m-%d %T') add_time,COUNT(qu.id) count " +
                "  from questionnaire q LEFT JOIN questionnaire_users qu on(qu.questionnaire_id=q.id) " +
                " LEFT JOIN questionnaire_title qt ON ( qt.questionnaire_id = q.id ) " +
                " where school_id = ? and created_by_id = ?  GROUP BY q.id ORDER BY q.id desc";

        //查询教师问卷信息
        List<Map<String,Object>> list = questionnaireJdbc.queryListMap(sql,schoolId,teacherId);
        //循环问卷信息
        for (Map<String,Object> map:list) {
            //查询这个问卷的答案
            sql = "select GROUP_CONCAT(answer) string from questionnaire_users_answer where questionnaire_title_id =?";
            //这个问卷的所有答案
            String answer = questionnaireJdbc.queryString(sql,new Object[]{map.get("id")});
            //答案map集合
            Map<String,Integer> answerMap= MapUtil.compress( MapUtil.compress(answer),map.get("questionnaire_option").toString());
            map.put("answerMap",answerMap);

            //查询没歌学生的调查
            sql = "select GROUP_CONCAT(qua.answer) answer,s.student_name from questionnaire_users_answer qua LEFT JOIN" +
                    " student s on (s.id=qua.student_id) where qua.questionnaire_title_id =? GROUP BY qua.student_id";
            //学生问卷调查
            List<Map<String,Object>> studentList = questionnaireJdbc.queryListMap(sql,map.get("id"));
            //循环解析学生问卷
            for (Map<String,Object> mapTwo :studentList) {
                mapTwo.put("answer",MapUtil.compress(answer));
            }
            map.put("studentList",studentList);

        }

        return new Result(list);
    }
}
